﻿using MaiPlanServer.IRepository;
using MaiPlanServer.IServices.IOrganizationaServices;
using MaiPlanServer.Model;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace MaiPlanServer.Services.OrganizationalServices
{
    public class PostManageServices : IPostManageServices
    {
        public PostManageServices(IBaseRepository BaseConn)
        {
            this.BaseConn = BaseConn;
        }

        public IBaseRepository BaseConn { get; }

        public async Task<bool> AddAsync(sys_post model)
        {
            var result = await BaseConn.InsertAsync(model);
            return result;
        }

        public async Task<bool> DellAllAsync(string[] post_id)
        {
            string sql = "delete sys_post where post_id in @post_id";
            var result = await BaseConn.DeleteAsync(sql, new { post_id });
            return result;
        }

        public async Task<bool> DellAsync(string post_id)
        {
           string sql = "delete sys_post where post_id=@post_id";
            var result = await BaseConn.DeleteAsync(sql, new {post_id });
            return result;
        }

        public async Task<int> GetCountAsync(string post_name)
        {
            string sql = @"select count(1) from sys_post where 1=1 and post_name like @post_name ";

            var count = await BaseConn.GetRecoreCountAsync(sql, new { post_name = "%" + post_name + "%" });

            return count;
        }

        public async Task<IEnumerable<sys_post>> GetPost(int PageIndex, int PageSize, string post_code, string post_name, string status)
        {
            string sql = @"select * from sys_post where 1=1 and post_code like @post_code and post_name like @post_name ";
            if (!string.IsNullOrEmpty(status))
            {
                sql += " and status = @State ";
            }
            sql += " order by Create_Time desc offset @PageSize*(@PageIndex-1) rows fetch next @PageSize row only";

            var list = await BaseConn.QueryAsync<sys_post>(sql, new { PageIndex, PageSize, post_code = "%" + post_code + "%", post_name = "%" + post_name + "%", State= status });

            return list;
        }

        public async Task<bool> UpdateAsync(sys_post model)
        {
            var result = await BaseConn.UpdateAsync(model, "post_id") ;
            return result;
        }
    }
}
